package com.fanshuai.spring.boot.config;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.transaction.UserTransaction;
import java.util.Properties;

@Configuration
public class AtomikosConfig {
    @Value("${db.test.url}")
    private String urlTest;

    @Value("${db.test.user}")
    private String usernameTest;

    @Value("${db.test.password}")
    private String passwordTest;

    @Value("${db.test2.url}")
    private String urlTest2;

    @Value("${db.test2.user}")
    private String usernameTest2;

    @Value("${db.test2.password}")
    private String passwordTest2;

    @Value("${druid.xa.enabled:false}")
    private boolean druidXAEnabled;

    private Properties get(String url, String username, String password) {
        Properties properties = new Properties();
        properties.put("driverClassName", "com.mysql.cj.jdbc.Driver");
        properties.put("url", url);
        properties.put("username", username);
        properties.put("password", password);
        properties.put("maxActive", 100);
        properties.put("minIdle", 5);
        properties.put("maxWait", 1000);
        properties.put("timeBetweenEvictionRunsMillis", 1000);
        properties.put("testOnBorrow", true);
        properties.put("testOnReturn", true);
        properties.put("testWhileIdle", true);

        return properties;
    }

    @Bean(name = "dataSourceBean1")
    public AtomikosDataSourceBean dataSourceBean1() {
        Properties p = get(urlTest, usernameTest, passwordTest);

        AtomikosDataSourceBean dataSourceBean = new AtomikosDataSourceBean();
        dataSourceBean.setUniqueResourceName("db-test");
        dataSourceBean.setXaDataSourceClassName(DruidXADataSource.class.getName());
        dataSourceBean.setXaProperties(p);

        return dataSourceBean;
    }

    @Bean(name = "dataSourceBean2")
    public AtomikosDataSourceBean dataSourceBean2() {
        Properties p = get(urlTest2, usernameTest2, passwordTest2);

        AtomikosDataSourceBean dataSourceBean = new AtomikosDataSourceBean();
        dataSourceBean.setUniqueResourceName("db-test2");
        dataSourceBean.setXaDataSourceClassName(DruidXADataSource.class.getName());
        dataSourceBean.setXaProperties(p);

        return dataSourceBean;
    }

    /**
     * jta事务管理器  @Transactional注解支持此管理器，也可使用TransactionManager, TransactionStatus方法
     * @return
     */
    @Bean(name = "jtaTransactionManager")
    public JtaTransactionManager jtaTransactionManager() {
        JtaTransactionManager manager = new JtaTransactionManager();

        UserTransaction userTransaction = new UserTransactionImp();
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        manager.setTransactionManager(userTransactionManager);
        manager.setUserTransaction(userTransaction);

        return manager;
    }
}
